<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编程式导航</title>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
    <style>

    </style>
</head>

<body>
    <div id="app">
        <h1>Hello App!</h1>
        <button @click="naviByStr">字符串</button>
        <button @click="naviByObj">对象</button>
        <button @click="naviByParams">参数的</button>
        <button @click="naviByQuery">查询参数</button>
        <router-view></router-view>
    </div>
</body>
<script>

    const User = {
        template: `
        <div>
            <div>用户</div>
            <router-view></router-view>
            <button @click="$router.go(-1)">go返回</button>
            <button @click="$router.back()">back返回</button>
            <button @click="$router.forward()">forward</button>
        </div>    
        ` 
    };

    const UserDetail = {
        template:`
        <div>
            <p>用户详情</p>
            <p>{{$route.params.id}}</p>
        </div>
        `
    }


    const routes = [
        {
            // name:"user",
            path: '/user',
            component: User,
        },
        {
            path:"/user-detail/:id",
            component:UserDetail,
            name:'detail'
        }
    ]

    // 3. 创建 router 实例，然后传 `routes` 配置
    const router = new VueRouter({
        routes // (缩写) 相当于 routes: routes
    })

    // 4. 创建和挂载根实例。
    // 记得要通过 router 配置参数注入路由，
    // 从而让整个应用都有路由功能
    const app = new Vue({
        router,
        methods: {
            // 字符串  path的路径
            naviByStr(){
                this.$router.push("user");
                // this.$router.push("/user");
            },
            // 对象
            naviByObj(){
                this.$router.push({
                    path:"user"
                });
            },
            // 参数  注意：如果提供了 path，params 会被忽略
            naviByParams(){
                this.$router.push({
                    name:"detail",
                    // path:"/user-detail/12",//使用这个，params中的id：456
                    params:{
                        id:456,
                    }
                })
            },
            // 查找
            naviByQuery(){
                this.$router.push({
                    // path:"user",
                    name:"detail",
                    query:{
                        id:456,
                        name:"小明"
                    }
                });
            }

        },
    }).$mount('#app')

// 现在，应用已经启动了！
</script>

</html>